/**
 *It is a demo for practicing JAVA. All codes are opened source by Jian.
 */
package jian.tool.daocode.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author Jian
 * @date 2012-6-24
 * @version
 * 
 */
public class JdbcUtil
{
    private static Properties prop = new Properties();

    static
    {
        InputStream in = null;
        try
        {
            in = new FileInputStream("config/db.properties");
            prop.load(in);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            try
            {
                in.close();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
            System.exit(1);
        }
        finally
        {
        }
    }

    private JdbcUtil()
    {

    }

    /**
     * 
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConn() throws ClassNotFoundException,
            SQLException
    {
        Class.forName(prop.getProperty("driverName"));
        Connection conn = DriverManager.getConnection(prop.getProperty("url"),
                                                      prop.getProperty("userName"),
                                                      prop.getProperty("password"));
        return conn;
    }

    /**
     * 
     * @param dbName
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConn(String dbName)
            throws ClassNotFoundException, SQLException
    {
        Class.forName(prop.getProperty("driverName"));
        Connection conn = DriverManager.getConnection(prop.getProperty("url")
                                                              + "/" + dbName,
                                                      prop.getProperty("userName"),
                                                      prop.getProperty("password"));
        return conn;
    }
    

    public static void close(ResultSet rs, Statement stmt, Connection conn)
    {
        if (rs != null)
        {
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        if (stmt != null)
        {
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        if (conn != null)
        {
            try
            {
                conn.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

}
